<?php

namespace app\data\command;

use app\data\service\CityUserRelationService;
use app\data\service\DailyOutputService;
use think\admin\Command;
use think\admin\Exception;
use think\console\Input;
use think\console\Output;
use think\facade\Log;

/**
 * 产出分析
 * Class ReportDailyOutput
 * @package app\data\command
 */
class ReportDailyOutput extends Command
{
    protected function configure()
    {
        $this->setName('report_daily_output');
        $this->setDescription('产出分析');
    }

    /**
     * 业务指令执行
     * @param Input $input
     * @param Output $output
     * @return void
     * @throws Exception
     */
    protected function execute(Input $input, Output $output)
    {
//        $startDate = new \DateTime('2023-06-16');
//        $endDate = new \DateTime('2023-09-18');
//
//        $interval = new \DateInterval('P1D');
//        $dateRange = new \DatePeriod($startDate, $interval, $endDate);
//
//        foreach ($dateRange as $date) {
//            $minTime = $date->format('Y-m-d');
//            $maxTime = $date->format('Y-m-d 23:59:59');
//
//            echo $minTime . PHP_EOL;
//            $this->formatDate($minTime, $maxTime);
//        }
//
//        die;

//        $start_date = '2023-06-16';
//        $end_date = '2023-11-23';
//
//        $current_date = $start_date;
//        while ($current_date <= $end_date) {
//
//            $this->app->db->name('report_daily_output')->where('date', $current_date)->delete();
//            DailyOutputService::instance()->formatDate($current_date, $current_date . ' 23:59:59');
//
//            echo $current_date . PHP_EOL;
//            $current_date = date('Y-m-d', strtotime($current_date . ' +1 day'));
//        }
//die;

        Log::info('产出分析-开始');

        $date = date('Y-m-d', strtotime('-1 day'));
        $maxTime = date('Y-m-d 23:59:59', strtotime('-1 day'));

        $isExist = $this->app->db->name('report_daily_output')->where('date', $date)->findOrEmpty();
        if ($isExist) {
            exit();
        }

        DailyOutputService::instance()->formatDate($date, $maxTime);

        Log::info('产出分析-结束');
    }

}